<?php
class SkillsController extends AppController {

	var $name = 'Skills';

	function index() {
		$this->Skill->recursive = 0;
		$this->set('skills', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid skill', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('skill', $this->Skill->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Skill->create();
			if ($this->Skill->save($this->data)) {
				$this->Session->setFlash(__('The skill has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The skill could not be saved. Please, try again.', true));
			}
		}
		$cvs = $this->Skill->Cv->find('list');
		$this->set(compact('cvs'));
	}

        function add_by_label($label = null) {
            if (!empty($label)) {
                $this->Skill->create();
                $category['Skill']['label'] = $label;
                if ($this->Skill->save($category)) {
                    return $this->Skill->id;
                } else {
                        return null;
                }
            }
            return null;
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid skill', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Skill->save($this->data)) {
				$this->Session->setFlash(__('The skill has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The skill could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Skill->read(null, $id);
		}
		$cvs = $this->Skill->Cv->find('list');
		$this->set(compact('cvs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for skill', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Skill->delete($id)) {
			$this->Session->setFlash(__('Skill deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Skill was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
        function delete_by_cv($id = null) {

            if(!empty ($id)) :
                $idSkills = $this->Skill->query('SELECT *
                                                       FROM skills_cvs sc
                                                       WHERE cv_id ='.$id);
                if(count($idSkills)>0) :
                    foreach ($idSkills as $skill) :
                        $this->Skill->delete(array('id' => $skill['sc']['skill_id']));
                    endforeach;

                    $this->Skill->query('DELETE FROM skills_cvs
                                           WHERE cv_id ='.$id);
                endif;
            endif;
        }
	function admin_index() {
		$this->Skill->recursive = 0;
		$this->set('skills', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid skill', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('skill', $this->Skill->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Skill->create();
			if ($this->Skill->save($this->data)) {
				$this->Session->setFlash(__('The skill has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The skill could not be saved. Please, try again.', true));
			}
		}
		$cvs = $this->Skill->Cv->find('list');
		$this->set(compact('cvs'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid skill', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Skill->save($this->data)) {
				$this->Session->setFlash(__('The skill has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The skill could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Skill->read(null, $id);
		}
		$cvs = $this->Skill->Cv->find('list');
		$this->set(compact('cvs'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for skill', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Skill->delete($id)) {
			$this->Session->setFlash(__('Skill deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Skill was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>